package com.zjf.fupload.popup.actions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.zjf.fupload.FUploadPlugin;
import com.zjf.fupload.entity.ServerInfo;

/**
 * 选择服务器对话框
 * @author zjf
 */
public class ChooseServerDialog extends Dialog {
	
	/**
	 * 对话框主界面
	 */
	private Composite container = null;
	/**
	 * 服务器列表
	 */
	private Combo serversCombo = null;
	private Text server = null;
	private Text port = null;
	private Text user = null;
	private Text pwd = null;
	private Text path = null;
	private ServerInfo info = null;
	
	/**
	 * 服务器名称->服务器{@link ServerInfo}的映射
	 */
	private Map<String, ServerInfo> serverMap = new HashMap<String, ServerInfo>();
	
	private List<String> serverNames = new ArrayList<String>();

	public ChooseServerDialog(Shell parentShell) {
		super(parentShell);
	}
	
	@Override
	protected Point getInitialSize(){
        return new Point(360, 240);//宽 高
    }
	
	/**
	 * 保存选择的服务器，以便后续使用
	 */
	@Override
	protected void okPressed() {
		String serverName = serversCombo.getItem(serversCombo.getSelectionIndex());
		ServerInfo info = serverMap.get(serverName);
		if(info == null) {
			info = new ServerInfo();
	    }
		info.setServer(server.getText().trim());
     	info.setPort(Integer.parseInt(port.getText().trim()));
     	info.setUsername(user.getText().trim());
     	info.setPassword(pwd.getText());
     	info.setPath(path.getText().trim());
		this.info = info;
		super.okPressed();
	}

	/**
	 * @see Dialog#createDialogArea(Composite)
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		
		serverMap.clear();
		serverNames.clear();
		String[] serverInfos = FUploadPlugin.getDefault().getServersPreference();
		for(String serverInfo : serverInfos) {
			ServerInfo info = ServerInfo.parse(serverInfo);
			if(info != null) {
				serverMap.put(info.getName(), info);
				serverNames.add(info.getName());
			}
		}
		Collections.sort(serverNames);
		
		//界面元素
		this.getShell().setText(FUploadPlugin.NAME);
		container = (Composite) super.createDialogArea(parent);
        container.setLayout(null);
        Label label = new Label(container, SWT.NULL);
        label.setText("选择服务器: ");
        label.setBounds(18, 20, 80, 20);
        serversCombo = new Combo(container, SWT.DROP_DOWN | SWT.BORDER);
        serversCombo.setBounds(100, 16, 200, 20);
        //设置下拉列表项 
        serversCombo.setItems(serverNames.toArray(new String[0]));
        
        Label serverLabel = new Label(container, SWT.NULL);
        server = new Text(container, SWT.SINGLE | SWT.BORDER);
        Label portLabel = new Label(container, SWT.NULL);
        port = new Text(container, SWT.SINGLE | SWT.BORDER);
        
        portLabel.setText("端口: ");
        serverLabel.setText("服务器地址: ");
        serverLabel.setBounds(18, 45, 80, 20);
        server.setBounds(100, 45, 160, 20);
        portLabel.setBounds(270, 45, 30, 20);
        port.setBounds(300, 45, 30, 20);
        
        Label userLabel = new Label(container, SWT.NULL);
        user = new Text(container, SWT.SINGLE | SWT.BORDER);
        
        userLabel.setText("用户名: ");
        userLabel.setBounds(18, 70, 80, 20);
        user.setBounds(100, 70, 200, 20);
        
        Label pwdLabel = new Label(container, SWT.NULL);
        pwd = new Text(container, SWT.SINGLE | SWT.BORDER);
        
        pwdLabel.setText("密码: ");
        pwdLabel.setBounds(18, 95, 80, 20);
        pwd.setBounds(100, 95, 200, 20);
        
        Label pathLabel = new Label(container, SWT.NULL);
        path = new Text(container, SWT.SINGLE | SWT.BORDER);
        
        pathLabel.setText("路径: ");
        pathLabel.setBounds(18, 120, 80, 20);
        path.setBounds(100, 120, 200, 20);
        
        //服务器列表选择事件
        serversCombo.addSelectionListener(new SelectionListener() {
        	
            public void widgetSelected(SelectionEvent e) {
            	ChooseServerDialog.this.update();
            	//System.out.println("Selected index: " + combo.getSelectionIndex() + ", selected item: " + combo.getItem(combo.getSelectionIndex()) + ", text content in the text field: " + combo.getText());
            }

            public void widgetDefaultSelected(SelectionEvent e) {
              System.out.println("Default selected index: " + serversCombo.getSelectionIndex() + ", selected item: " + (serversCombo.getSelectionIndex() == -1 ? "<null>" : serversCombo.getItem(serversCombo.getSelectionIndex())) + ", text content in the text field: " + serversCombo.getText());
              String text = serversCombo.getText();
              if(serversCombo.indexOf(text) < 0) { // Not in the list yet. 
                serversCombo.add(text);
                // Re-sort
                String[] items = serversCombo.getItems();
                Arrays.sort(items);
                serversCombo.setItems(items);
              }
            }
          });
        
        serversCombo.select(0);
        update();
        
        return container;
    }
	
	/**
	 * 将选择的服务器信息更新至界面元素中。
	 */
	private void update() {
		String serverName = serversCombo.getItem(serversCombo.getSelectionIndex());
		ServerInfo info = serverMap.get(serverName);
        if(info != null) {
        	server.setText(info.getServer());
        	port.setText(info.getPort() + "");
        	user.setText(info.getUsername());
        	pwd.setText(info.getPassword());
        	path.setText(info.getPath());
        }
	}
	
	/**
	 * 获取保存的服务器
	 * @return
	 * 		{@link ServerInfo}
	 */
	public ServerInfo getServerInfo() {
		return this.info;
	}

}
